package com.waitingresult.meituan;

import java.util.*;

/**
 * @Author: WangShuai
 * @CreateTime: 2022-11-01  18:25
 * @Description: TODO
 * @Version: 1.0
 */
public class MeituanMain {

    /**
     * 将订单里订单子项中商品销量进行统计，方便商家分析改善运营策略；
     * 比如 OrderItem
     * 案例1:{"sku":{"id":1, "name":"蒸蛋"}, "makeMethods":["少盐","免味精"],"count":1}
     * 案例2:{"sku":{"id":1, "name":"蒸蛋"}, "makeMethods":["免味精","少盐"],"count":1}
     * 案例1和案例2的订单子项的商品都一样，进行合并统计，合并统计后:
     * {"sku":{"id":1, "name":"蒸蛋"},"count":2}
     * --------请完成统计商家销量最高的N个商品代码---------
     * 要求：
     * 1、完成统计销量前N名商品的方法；
     * 2、方法名考生自行定义;
     * 3、已知入参有代表商家想统计销量前N名的变量n，订单列表List<Order> orders；
     * 4、方法返回的数据结构考生自行定义；必须包括商品信息、销量信息;
     */

    public static void main(String[] args) {
        // 组装数据
        List<Order> orders = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Order order = new Order();
            ArrayList<OrderItem> orderItems = new ArrayList<>();
            for (int j = 1; j < 10; j++) {
                Sku sku = new Sku(j, j + "号商品");
                orderItems.add(new OrderItem(sku, j, Collections.singletonList("")));
            }
            order.setOrderItems(orderItems);
            orders.add(order);
        }

        List<SkuOrder> search = search(3, orders);
        System.err.println(search);
    }

    public static List<SkuOrder> search(int count, List<Order> orders) {
        // list 转map
        HashMap<Integer, Integer> map = getMap(orders);


        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        // sku ID 当做key ， entity 当做value
        HashMap<Integer, Sku> integerSkuHashMap = new HashMap<>();
        for (Order order : orders) {
            for (OrderItem orderItem : order.getOrderItems()) {
                integerSkuHashMap.put(orderItem.getSku().getId(), orderItem.getSku());
            }
        }


        ArrayList<SkuOrder> skuOrders = new ArrayList<>();

        // 循环所有的销量
        for (Map.Entry<Integer, Integer> entry : entries) {
            Integer id = entry.getKey();
            Integer mapCount = entry.getValue();
            SkuOrder skuOrder = new SkuOrder();
            skuOrder.setSkuId(id);
            Sku sku = integerSkuHashMap.get(id);
            skuOrder.setSku(sku);
            skuOrder.setCount(mapCount);
            skuOrders.add(skuOrder);
        }
        //  return Integer.compare(e2.getAge(), e1.getAge());
        // 排序
        List<SkuOrder> collect = skuOrders.stream().sorted((o1, o2) -> Integer.compare(o2.getCount(), o1.getCount())).toList();
        // 截取
        return collect.subList(0, count);
    }

    private static LinkedHashMap<Integer, Integer> getMap(List<Order> orders) {
        LinkedHashMap<Integer, Integer> map = new LinkedHashMap<>();
        for (Order order : orders) {
            List<OrderItem> orderItems = order.getOrderItems();
            if (orderItems.isEmpty()) {
                continue;
            }
            for (OrderItem orderItem : orderItems) {
                Sku sku = orderItem.getSku();
                Integer id = sku.getId();
                Integer count1 = orderItem.getCount();
                map.merge(id, count1, Integer::sum);
            }
        }
        return map;
    }
}
